package com.glory.good_dark.adapter;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.good_dark.R;
import com.glory.good_dark.callback.StringCallback;
import com.glory.good_dark.entity.SystemConfigEntity;
import com.glory.good_dark.utils.PopUtils;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.util.List;

import static com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils.getConfirmAndCancelDialog;

public class StripIDListAdapter extends BaseQuickAdapter<SystemConfigEntity.SystemType, BaseViewHolder> {
    private AddAndDeleteCallback mCallback;
    private FragmentManager mFragmentManager;

    public StripIDListAdapter(@Nullable List<SystemConfigEntity.SystemType> data, FragmentManager fragmentManager ,AddAndDeleteCallback callback) {
        super(R.layout.adapter_strip_id,data);
        this.mCallback = callback;
        this.mFragmentManager = fragmentManager;
    }

    private FreeUI_GeneralPop lotIDPop;
    @Override
    protected void convert(BaseViewHolder helper, SystemConfigEntity.SystemType item) {
        helper.setText(R.id.tvLot,item.getLOT_ID())
                .setText(R.id.tvCount,item.getLF_Count())
                .setText(R.id.tvStripID,item.getStripInfo().size() > 0 ? item.getStripInfo().get(0) : "");
        item.setStripID(item.getStripInfo().size() > 0 ? item.getStripInfo().get(0) : "");
        FrameLayout flStripIDList = helper.getView(R.id.flStripIDList);
        ImageView ivStripIDListArrow = helper.getView(R.id.ivStripIDListArrow);
        flStripIDList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (item.getStripInfo().size() <= 0) {
                    FreeApi_ToastUtils.showFreeToast("料条ID列表为空",mContext,false, Toast.LENGTH_SHORT);
                    return;
                }
                lotIDPop = PopUtils.getNormalPop(mContext, flStripIDList.getWidth(), item.getStripInfo(), ivStripIDListArrow, new StringCallback() {
                    @Override
                    public void onSelectorCallback(String val, int position) {
                        helper.setText(R.id.tvStripID,val);
                        item.setStripID(val);
                    }

                    FreeUI_GeneralFragmentDialog dialog;
                    @Override
                    public void onDeleteCallback(String val, Object adapter) {
                        dialog = getConfirmAndCancelDialog(mContext, "是否确认删除当前料条ID?", new FreeUI_CommonCallback() {
                            @Override
                            public void onSuccess() {
                                dialog.dismiss();
                                mCallback.onDeleteCallback(item,val);
                            }
                        }, new FreeUI_CommonCallback() {
                            @Override
                            public void onSuccess() {
                                dialog.dismiss();
                            }
                        });
                        dialog.show(mFragmentManager,"DELETE_DIALOG");
                    }
                });
                PopUtils.setArrowShow(ivStripIDListArrow);
                lotIDPop.showAsDropDown(flStripIDList, 0, 0);
            }
        });

        helper.getView(R.id.btnAdd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (item.getStripID().equals("")){
                    FreeApi_ToastUtils.showFreeToast("请选择料条ID",mContext,false, Toast.LENGTH_SHORT);
                    return;
                }

                mCallback.onAddCallback(item);
            }
        });
    }

    public interface AddAndDeleteCallback{
        void onAddCallback( SystemConfigEntity.SystemType item);
        void onDeleteCallback( SystemConfigEntity.SystemType item,String stripID);
    }
}
